package com.example.h9iserver.common;


import lombok.Data;
import lombok.ToString;

import java.io.Serializable;

/**
 * @Description: 接口返回封装
 * @Author: Dabs
 * @Date: 2020/3/11 18:11
 */

@Data
@ToString
public class Result<T> implements Serializable {

    /**
     * 响应信息code
     */
    private boolean success;
    /**
     * 响应信息
     */
    private String errorMessage;

    /**
     * 响应信息http status
     */
    private Integer errorCode;


    private T data;

    public Result() {
    }

    public Result(ExceptionCodeConstants exceptionCodeConstants) {
        this.success = exceptionCodeConstants.getSuccess();
        this.errorMessage = exceptionCodeConstants.getErrorMessage();
        this.errorCode = exceptionCodeConstants.getErrorCode();
    }

    public Result(ExceptionCodeConstants exceptionCodeConstants, T data) {
        this.success = exceptionCodeConstants.getSuccess();
        this.errorMessage = exceptionCodeConstants.getErrorMessage();
        this.errorCode = exceptionCodeConstants.getErrorCode();
        this.data = data;
    }

    public Result(ExceptionCodeConstants exceptionCodeConstants, T data, String errorMessage) {
        this.success = exceptionCodeConstants.getSuccess();
        this.errorMessage = errorMessage;
        this.errorCode = exceptionCodeConstants.getErrorCode();
        this.data = data;
    }

    public Result(ExceptionCodeConstants exceptionCodeConstants, String errorMessage) {
        this.success = exceptionCodeConstants.getSuccess();
        this.errorMessage = errorMessage;
        this.errorCode = exceptionCodeConstants.getErrorCode();
    }

    public static <T> Result<T> success(){
        return new Result<>(ExceptionCodeConstants.SUCCESS, ExceptionCodeConstants.SUCCESS.errorMessage);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ExceptionCodeConstants.SUCCESS, data);
    }

    public static <T> Result<T> success(T data, String msg) {
        return new Result<>(ExceptionCodeConstants.SUCCESS, data, msg);
    }

    public static <T> Result<T> fail(T data, String msg) {
        return new Result<>(ExceptionCodeConstants.FAIL, data, msg);
    }

    public static <T> Result<T> fail(String msg) {
        return new Result<T>(ExceptionCodeConstants.FAIL, msg);
    }

    public static <T> Result<T> authFail(String msg) {
        return new Result<T>(ExceptionCodeConstants.FAIL_UNAUTHORIZED, msg);
    }

    public static <T> Result<T> fail() {
        return new Result<T>(ExceptionCodeConstants.FAIL, ExceptionCodeConstants.FAIL.errorMessage);
    }

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Integer getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(Integer errorCode) {
        this.errorCode = errorCode;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

}
